
//
//  XBCell.m
//  testTimer
//
//  Created by Binbin on 16/7/1.
//  Copyright © 2016年 Binbin. All rights reserved.
//

#import "XBCell.h"

@interface XBCell ()
@property (nonatomic,strong) NSTimer   * timer;
@end

@implementation XBCell

- (void)awakeFromNib {
    [super awakeFromNib];
//    [self timer];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setEndTimestamp:(NSNumber *)endTimestamp {
    _endTimestamp = endTimestamp;
    long long second = [self getSecond];
    if (second <= 0) {
        [self.timer invalidate];
        _timer = nil;
        self.lbl.text = @"时间到啦";
    } else {
        [self timer];
    }
}
- (long long )getSecond {
    NSDate * now = [NSDate date];
    NSNumber * nowTimeSp = @((long)[now timeIntervalSince1970]);
    long long second = (self.endTimestamp.longLongValue - nowTimeSp.longLongValue);
    return second;
}

- (void)refreshLbl:(NSTimer *)timer {
    long long second = [self getSecond];
    if (second <= 0) {

        self.lbl.text = @"时间到啦";
    } else {
        self.lbl.text = [NSString stringWithFormat:@"%lld秒/%lld分钟",second,second/60];
    }
}

- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshLbl:) userInfo:@"" repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
    }
    return _timer;
}


@end
